﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using System.Drawing;
using Silvermoon.Controls.Interfaces;

namespace Silvermoon.Controls
{
    public abstract class ItemBase : Control, IMouseEventBehavior, IContextMenuBehavior
    {

        #region fields
        private int height;
        #endregion
        #region Props

        private Rectangle actualBounds;

        /// <summary>
        /// Gets the actual clipping bounds that the renderer is using to determine the area in which to render this controls.
        /// </summary>
        public override Rectangle ActualBounds
        {
            get
            {
                return actualBounds.IsEmpty ? new Rectangle(location.X, location.Y, size.Width, size.Height) : actualBounds;
            }
            set { actualBounds = value; }
        }

        public ListViewBase ListView { get; internal set; }

        public bool IsFocused { get; private set; }

        public virtual int ActualHeight { get { return Height; } }

        public virtual bool IsSelected
        {
            get
            {
                return ListView != null ? ListView.IsItemSelected(this) : false;
            }
            set
            {
                if (value && ListView != null)
                {
                    if (value != IsSelected)
                    {
                        ListView.SelectItem(this);
                        OnSelectedChanged();
                        Invalidate(InvalidationFlag.Appearance);
                    }
                }
            }
        }

        /// <summary>
        /// Gets the index in the <see cref="ListView"/>, if attached, otherwise -1.
        /// </summary>
        public int ItemIndex
        {
            get
            {
                return ListView != null ? ListView.IndexOfItem(this) : -1;
            }
        }

        /// <summary>
        /// Gets whether the <see cref="ListViewItem"/> is attached to a <see cref="ListView"/>.
        /// </summary>
        public bool IsAttached { get { return ListView != null; } }

        #endregion
        #region methods

        protected override void OnSizeChanged()
        {
            if (height != Height)
            {
                height = Height;
                OnHeightChanged();
            }
            base.OnSizeChanged();
        }

        protected virtual void OnHeightChanged()
        {
            if (ListView != null) ListView.Items.UpdateHeight(this);
            if (HeightChanged != null) HeightChanged(this, EventArgs.Empty);
        }

        public virtual void Removing()
        {
        }

        public virtual void Inserting()
        {
        }

        public virtual void OnSelectedChanged()
        {
            if (this.IsSelected) ListView.SelectedItem = this;
        }

        public virtual void OnDetach(int index)
        {
        }

        #endregion
        #region events

        /// <summary>
        /// Occurs when the height has changed.
        /// </summary>
        public event EventHandler HeightChanged;

        public event EventHandler Click;


        #endregion
        #region IMouseEventBehavior Members

        public virtual void OnMouseDown(MouseDownEventArgs e)
        {
            e.Handled = true;
            IsSelected = true;
           // e.Cancel = true;
        }

        public virtual void OnMouseUp(MouseUpEventArgs e)
        {
            e.AllowDoubleClick = false;
            e.Handled = true;
        }

        public virtual void OnMouseClick(MouseClickEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
                switch (e.MouseClickType)
                {
                    case MouseClickType.SingleClick:
                        OnClick();
                        break;
                }
            }
        }

        public virtual void OnClick()
        {
            if (Enabled)
            {
                if (ListView != null) ListView.OnItemClick(this);
                if (Click != null) Click(this, EventArgs.Empty);
            }
        }


        public virtual void OnMouseMove(MouseMoveEventArgs e)
        {
        }

        public virtual void OnGotFocus()
        {
            IsFocused = true;
        }

        public virtual void OnLostFocus()
        {
            IsFocused = false;
        }

        #endregion
        #region IContextMenu Members

        public ContextMenu ContextMenu { get; set; }

        #endregion
    }
}
